$(document).ready(function() {
	var _userForm = $('#form_user_add');
	var _editForm = $('#form_user_edit');

	$('#submit_form').click(function() {
		var objUserForm = {
			_robot: $("#robot"),
			_source: $("#source"),
			_url: $("input[name='url']", _userForm),
			_date: $("input[name='date']", _userForm),
			_article: $("#article")
		}

		for (var i in objUserForm) {
			if (objUserForm[i].val() == '' || objUserForm[i].val() == null) {
				objUserForm[i].css({
					'border-color': 'red'
				});
				//layer.tips('必填项',objUserForm[i]);
				return false;
			} else {
				objUserForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
			//alert(objUserForm[i].val());
		}
		$.ajax({
			type: "POST",
			url: "/Logs/addColl",
			dataType: "JSON",
			async: true,
			data: {
				"robot": objUserForm._robot.val(),
				"source": 　objUserForm._source.val(),
				"url": objUserForm._url.val(),
				"date": objUserForm._date.val(),
				"article": objUserForm._article.val()
			},
			success: function(data) {
				window.alert(data.info);
				if (true === data.status) window.location = "/Logs/user";
			},
			error: function(e) {

				console.log(e);
			}


		});
	});

	$('.userDelete').click(function() {
		if (window.confirm("确认删除？")) {
			var userId = $(this).parent('td').attr('item_id');
			$.ajax({
				type: "GET",
				url: $(this).attr('href'),
				async: true,
				data: {
					//"id" : userId
				},
				success: function(data) {
					window.alert(data.info);
					if (true === data.status) window.location = "/Logs/coll";
				}
			});
			return false;
		} else {
			return false;
		}
	});

	$('.userEdit').click(function() {
		$('#modal_edit').modal('toggle');
		var _parent = $(this).parent('td').parent('tr.gradeX');
		userId = $(this).parent('td').attr('item_id');
		robot_id = $(this).parent('td').attr('robot_id');
		source_id = $(this).parent('td').attr('source_id');
		article_id = $(this).parent('td').attr('article_id');
		objEditForm = {
			_robot: $("#robot", _editForm),
			_source: $("#source", _editForm),
			_url: $("input[name='url']", _editForm),
			_article: $("#article", _editForm),
			_date: $("input[name='date']", _editForm)
		};
		var tdTable = [];
		$('td', _parent).each(function() {
			tdTable.push($(this).text());
		});
		objEditForm._url.val(tdTable[2]);
		objEditForm._date.val(tdTable[4]);
		$('option', objEditForm._robot).each(function() {
			if ($(this).val() == robot_id) {
				$(this).attr('selected', true);
			}
		});
		$('option', objEditForm._article).each(function() {
			if ($(this).val() == article_id) {
				$(this).attr('selected', true);
			}
		});
		$('option', objEditForm._source).each(function() {
			if ($(this).val() == source_id) {
				$(this).attr('selected', true);
			}
		});
		return false;
	});
	$('#submitEdit').click(function() {
		for (var i in objEditForm) {
			if (objEditForm[i].val() == '' || objEditForm[i].val() == null) {
				objEditForm[i].css({
					'border-color': 'red'
				});
				return false;
			} else {
				objEditForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
		}
		$.ajax({
			type: "POST",
			url: "/Logs/editColl/id/" + userId,
			async: true,
			data: {
				"article": objEditForm._article.val(),
				"robot": 　objEditForm._robot.val(),
				"source": objEditForm._source.val(),
				"url": objEditForm._url.val(),
				"date": objEditForm._date.val()
			},
			success: function(data) {
				window.alert(data.info);
				if (true === data.status) window.location = "/Logs/coll";
			}
		});
	});

	$(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
		var urlOld = $(this).attr('href'),
			urlArr = urlOld.split("/");
		urlId = urlArr[urlArr.length - 1];
		$.ajax({
			type: 'POST',
			url: '/Logs/collPage/page/' + urlId,
			data: urlId,
			success: function(data) {
				var data = $.parseJSON(data);

				$('#tbody').html('');
				var id = '';

				$.each(data, function(k, v) {

					id = v['authority_id'];

					$('#tbody').append($('<tr class="gradeX"><td>' + v['coll_startTime'] + '</td><td>' + v['typical_name'] + '</td><td>' + v['level_status'] + '</td><td>' + v['location'] + '</td><td>' + v['message'] + '</td><td>' + v['thread'] + '</td><td>' + v['coll_endTime'] + '</td></tr>'));



				});


			}

		});
		$.ajax({
			type: 'POST',
			url: '/Logs/collCurrentPage/page/' + urlId,
			data: urlId,
			success: function(data) {
				console.log(data);
				$('#pages').html(data);
				$('#pages').ready(function() {
					nextBtn = $('#page_next');
				});
			}

		});
		return false;
	});
});